<?php
$module = $this->moduleName;
$method = $this->methodName;
js::set('themeRoot', $themeRoot);
js::set('jsRoot', $jsRoot);
if(!isset($config->$module->editor->$method)) return;
$editor = $config->$module->editor->$method;
$editor['id'] = explode(',', $editor['id']);
$editorLangs  = array('en' => 'en', 'zh-cn' => 'zh_CN', 'zh-tw' => 'zh_TW');
$editorLang   = isset($editorLangs[$app->getClientLang()]) ? $editorLangs[$app->getClientLang()] : 'en';
?>
<link rel="stylesheet" href="<?php echo $jsRoot;?>editormd/css/editormd.min.css" />
<link rel="stylesheet" href="<?php echo $jsRoot;?>editormd/css/editormd.logo.min.css" />
<link rel="stylesheet" href="<?php echo $jsRoot;?>editormd/css/editormd.preview.min.css" />
<script src='<?php echo $jsRoot;?>editormd/editormd.js' type='text/javascript'></script>
<script language='javascript'>
var editor = <?php echo json_encode($editor);?>;
var Editor={};


//var bugTools =
//[ 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic','underline', '|',
//'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', '|',
//'emoticons', 'image', 'code', 'link', '|', 'removeformat','undo', 'redo', 'fullscreen', 'source', 'about'];
//
//var simpleTools =
//[ 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic','underline', '|',
//'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', '|',
//'emoticons', 'image', 'code', 'link', '|', 'removeformat','undo', 'redo', 'fullscreen', 'source', 'about'];
//
//var fullTools =
//[ 'formatblock', 'fontname', 'fontsize', 'lineheight', '|', 'forecolor', 'hilitecolor', '|', 'bold', 'italic','underline', 'strikethrough', '|',
//'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', '|',
//'insertorderedlist', 'insertunorderedlist', '|',
//'emoticons', 'image', 'insertfile', 'hr', '|', 'link', 'unlink', '/',
//'undo', 'redo', '|', 'selectall', 'cut', 'copy', 'paste', '|', 'plainpaste', 'wordpaste', '|', 'removeformat', 'clearhtml','quickformat', '|',
//'indent', 'outdent', 'subscript', 'superscript', '|',
//'table', 'code', '|', 'pagebreak', 'anchor', '|',
//'fullscreen', 'source', 'preview', 'about'];
$(document).ready(initKindeditor);
function initKindeditor(afterInit)
{
    var nextFormControl = 'input:not([type="hidden"]), textarea:not(.ke-edit-textarea), button[type="submit"], select';
    $.each(editor.id, function(key, editorID)
    {
        var editor = $('#' + editorID);
        var value=editor.val();
//        setType('text');
//        $("input[name=type]").val('text');
        var dom=$('#typetext');
        if(dom!=undefined){
            dom.click();
        }
//        editor.parent().parent().show();
//        editor.parent().parent().hide();
        editor.parent().append('<div id="'+editorID+'"></div>');
        editor.remove();

        Editor[key]= editormd(editorID, {
            width: "90%",
            height: 240,
            path : jsRoot+'editormd/lib/',
            theme : "dark",
            previewTheme : "dark",
            editorTheme : "pastel-on-dark",
            markdown : value,
            codeFold : false,
            //syncScrolling : false,
            saveHTMLToTextarea : false,    // 保存 HTML 到 Textarea
            searchReplace : true,
            watch : false,                // 关闭实时预览
            htmlDecode : "style,script,iframe|on*",            // 开启 HTML 标签解析，为了安全性，默认不开启
            toolbar  : true,             //关闭工具栏
            previewCodeHighlight : true, // 关闭预览 HTML 的代码块高亮，默认开启
            emoji : true,
            taskList : true,
            tocm : true,         // Using [TOCM]
            tex : true,                   // 开启科学公式TeX语言支持，默认关闭
            flowChart : true,             // 开启流程图支持，默认关闭
            sequenceDiagram : true,       // 开启时序/序列图支持，默认关闭,
            //dialogLockScreen : false,   // 设置弹出层对话框不锁屏，全局通用，默认为true
            //dialogShowMask : false,     // 设置弹出层对话框显示透明遮罩层，全局通用，默认为true
            //dialogDraggable : false,    // 设置弹出层对话框不可拖动，全局通用，默认为true
            //dialogMaskOpacity : 0.4,    // 设置透明遮罩层的透明度，全局通用，默认值为0.1
            //dialogMaskBgColor : "#000", // 设置透明遮罩层的背景颜色，全局通用，默认为#fff
            imageUpload : true,
            imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
            imageUploadURL : "<?php echo $jsRoot;?>editormd/php/upload.php",
            onload : function() {
                console.log('onload', this);
//                console.log('dddd',this.markdownTextarea);
                this.markdownTextarea.attr('name',this.id);
                //this.fullscreen();
                //this.unwatch();
                //this.watch().fullscreen();

                //this.setMarkdown("#PHP");
                //this.width("100%");
                //this.height(480);
//                this.resize("100%", 240);
//                console.log(editor);

            },
            onresize :function(){
//                this.resize("100%", 240);
//                var editor           = this.editor;
//                console.log(editor);
            }
        });

    });
    if($.isFunction(afterInit)) afterInit();
}
</script>
